package com.jumi.microservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jumi.microservice.common.constant.RequestConstant;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.constant.StatusEnum;
import com.jumi.microservice.domain.RotationChart;
import com.jumi.microservice.domain.dto.RotationChartDTO;
import com.jumi.microservice.domain.vo.RotationChartVO;
import com.jumi.microservice.mapper.RotationChartMapper;
import com.jumi.microservice.service.RotationChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Transactional(rollbackFor = Exception.class)
public class RotationChartServiceImpl extends ServiceImpl<RotationChartMapper, RotationChart> implements RotationChartService {
    @Resource
    private RotationChartMapper rotationChartMapper;

    @Override
    public RotationChartDTO selectById(Long id) {
        //判断传过来的数据是否为空
        ExceptionEnum.FAIL_ID_NULL.doThrowIf(id==null);
        //查询id对应的数据
        RotationChart rotationCharts = rotationChartMapper.selectById(id);
        ExceptionEnum.FAIL_NULL.doThrowIf(rotationCharts==null);
        return po2Dto(rotationCharts);
    }

    @Override
    public void insert(RotationChartDTO dto, HttpServletRequest request) {
        //判断传过来的数据是否为空
        ExceptionEnum.FAIL_NULL.doThrowIf(dto==null);
        //判断标题不能重复
        int titleCount = rotationChartMapper.selectCount(
                Wrappers.<RotationChart>lambdaQuery()
                        .eq(RotationChart::getTitle,dto.getTitle())
                        .eq(RotationChart::getIsDel, StatusEnum.STATUS_NO.getCode())
        );
        ExceptionEnum.FAIL_ROTATION_CHART_TITLE.doThrowIf(titleCount > 0);

        RotationChart rotationChart = dto2Po(dto);
        rotationChart.setCreater(request.getHeader(RequestConstant.ADMIN_NAME));
        rotationChart.setModifier(request.getHeader(RequestConstant.ADMIN_NAME));

        baseMapper.insert(rotationChart);
    }

    @Override
    public void update(RotationChartDTO dto, HttpServletRequest request) {
        ExceptionEnum.FAIL_NULL.doThrowIf(dto==null);
        RotationChart chart = rotationChartMapper.selectById(dto.getId());
        ExceptionEnum.FAIL_NULL.doThrowIf(chart==null);
        int titleCount = rotationChartMapper.selectCount(
                Wrappers.<RotationChart>lambdaQuery()
                .ne(RotationChart::getId,dto.getId())
                .eq(RotationChart::getTitle,dto.getTitle())
                .eq(RotationChart::getIsDel,StatusEnum.STATUS_NO.getCode())
        );
        ExceptionEnum.FAIL_ROTATION_CHART_TITLE.doThrowIf(titleCount > 0);

        RotationChart rotationChart = dto2Po(dto);
        rotationChart.setModifier(request.getHeader(RequestConstant.ADMIN_NAME));
        rotationChartMapper.updateById(rotationChart);
    }

    @Override
    public void delete(Long id,HttpServletRequest request) {
        ExceptionEnum.FAIL_ID_NULL.doThrowIf(id==null);
        RotationChart chart = rotationChartMapper.selectById(id);
        ExceptionEnum.FAIL_NULL.doThrowIf(chart==null);
        chart.setIsDel(StatusEnum.STATUS_YES.getCode());
        chart.setModifier(request.getHeader(RequestConstant.ADMIN_NAME));
        rotationChartMapper.updateById(chart);
    }

    @Override
    public void enableStatus(Long id, Integer status, HttpServletRequest request) {
        ExceptionEnum.FAIL_ID_NULL.doThrowIf(id==null);
        RotationChart chart = rotationChartMapper.selectById(id);
        ExceptionEnum.FAIL_NULL.doThrowIf(chart==null);
        chart.setStatus(status);
        chart.setModifier(request.getHeader(RequestConstant.ADMIN_NAME));
        rotationChartMapper.updateById(chart);
    }

    @Override
    public List<RotationChartVO> selectRotationChartList() {
        LambdaQueryWrapper<RotationChart> wrapper = new LambdaQueryWrapper<>();
        wrapper .eq(RotationChart::getIsDel, 0);
        wrapper.orderByDesc(RotationChart::getSort, RotationChart::getUpdateTime);
        List<RotationChart> vos = rotationChartMapper.selectList(wrapper);
        return pos2Vos(vos);
    }

    /**
     * 对象转换 dto->do
     * @param dto
     * @return
     */
    private RotationChart dto2Po(RotationChartDTO dto){
        RotationChart r = null;
        try {
            r = dto.clone(RotationChart.class);
        }catch (Exception e){
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return r;
    }

    /**
     * 对象转换 po->dto
     * @param po
     * @return
     */
    private RotationChartDTO po2Dto(RotationChart po){
        RotationChartDTO r = null;
        try {
            r = po.clone(RotationChartDTO.class);
        }catch (Exception e){
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return r;
    }

    private List<RotationChartVO> pos2Vos(List<RotationChart> rotationCharts) {
        List<RotationChartVO> pos = rotationCharts.stream().map(p -> {
            RotationChartVO vo = null;
            try {
                vo = p.clone(RotationChartVO.class);
            } catch (Exception e) {
                ExceptionEnum.FAIL_CHANGE.newException();
            }
            return vo;
        }).collect(Collectors.toList());
        return pos;
    }
}
